package com.windbell.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;

import com.windbell.ContentActivity;
import com.windbell.R;
import com.windbell.layout.CustomListView;
import com.windbell.net.NetTask;

public class NewsFeed {
	private Context mContext;
	private Activity mActivity;
	private View mView;
	SimpleAdapter adapter;
	CustomListView listView;

	public NewsFeed(Application application, Context context) {
		mContext = context;
		mView = LayoutInflater.from(context).inflate(R.layout.activity_main,
				null);
		findViewById();
		initData();
	}

	List array = new ArrayList();

	public void initData() {
		for (int i = 0; i < 10; i++) {
			Map map = new HashMap();
			map.put("text", "line" + i);
			array.add(map);
		}
		adapter = new SimpleAdapter(mContext, array,
				android.R.layout.simple_list_item_single_choice,
				new String[] { "text" }, new int[] { android.R.id.text1 });
		listView.setAdapter(adapter);

		listView.setonRefreshListener(new CustomListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				new NetTask(new NetTask.myProcess() {
					@Override
					public void process(String result) {
						try {
							JSONObject json = new JSONObject(result);
							JSONArray jsonarray = (JSONArray) json.get("root");
							List list = new ArrayList();
							for (int i = 0; i < jsonarray.length(); i++) {
								Map map = new HashMap();
								map.put("text", ((JSONObject) jsonarray.get(i))
										.get("atitle"));
								map.put("aurl", ((JSONObject) jsonarray.get(i))
										.get("aurl"));
								list.add(map);
							}
							array.clear();
							array.addAll(list);
							adapter.notifyDataSetChanged();
							listView.onRefreshComplete();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

				}).execute("http://113.247.225.203:8080/serv/NewsAction?method=0");
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String aurl = ((Map) array.get(arg2)).get("aurl") + "";
				Intent i = new Intent();
				i.putExtra("aurl", aurl);
				i.setClass(mContext, ContentActivity.class);
				mContext.startActivity(i);
			}

		});
		listView.setonLoadListener(new CustomListView.OnLoadListener() {

			@Override
			public void onLoad() {
				new NetTask(new NetTask.myProcess() {
					@Override
					public void process(String result) {
						for (int i = 0; i < 10; i++) {
							Map map = new HashMap();
							map.put("text", "loading" + i);
							array.add(map);
						}
						adapter.notifyDataSetChanged();
						listView.onLoadComplete();
					}
				}).execute("http://www.baidu.com");
			}
		});

	}

	public void findViewById() {
		listView = (CustomListView) mView.findViewById(R.id.cListView);
	}

	public View getView() {
		return mView;
	}

}
